<?php

namespace Biz\module\Decoupling\data;

use Biz\module\Decoupling\AbstractMain;
use Biz\service\Event;
use Biz\service\Datastat as sDatastat;

class BackendOrderStatics extends AbstractMain
{
    public function main($message, $data)
    {
        $this->appendLog('订单数据统计事件[开始记录]', compact('message', 'data'));
        $body = [];
        $addtime = '';
        $eventTag = '';
        try {
            if (!isset($data['header']) || !isset($data['header']['event_tag'])) {
                throw new \Exception('header数据有误,data:' . json_encode($data));
            }
            if (!isset($data['body'])) {
                throw new \Exception('body数据有误,data:' . json_encode($data));
            }
            $eventTag = $data['header']['event_tag'];
            $body = $data['body'];
            $bodyString = json_encode($body, JSON_UNESCAPED_UNICODE);
            switch ($eventTag) {
                case Event::ORDER_CREATE_SUCCESS:
                    //必要参数验证
                    if (!isset($body['uid'])) throw new \Exception('body数据有误,data:' . $bodyString);
                    if (!isset($body['order_parent_no'])) throw new \Exception('body数据有误,data:' . $bodyString);
                    if (!isset($body['order_list'])) throw new \Exception('body数据有误,data:' . $bodyString);
                    if (!isset($data['header']['addtime'])) throw new \Exception('header数据有误,data:' . json_encode($data));
                    $uid = $body['uid'];
                    $order_parent_no = $body['order_parent_no'];
                    $order_list = $body['order_list'];
                    $addtime = $data['header']['addtime'];
                    sDatastat::createOrder($uid, $order_parent_no, $order_list, $addtime);
                    break;
                case Event::ORDER_PAY_SUCCESS:
                    if (!isset($body['order_info_list'])) throw new \Exception('body数据有误,data:' . $bodyString);
                    $order_info_list = $body['order_info_list'];
                    sDatastat::payOrder($order_info_list);
                    break;
                case Event::ORDER_REFUND_USER_APPLY_SUCCESS:
                    //必要参数验证
                if (!isset($body['order_no'])) throw new \Exception('body数据有误,data:' . $bodyString);
                    $order_no = $body['order_no'];
                    sDatastat::refundOrder($order_no);
                    break;
            }
        } catch (\Exception $e) {
            $this->appendLog('订单数据统计事件[参数校验]错误,信息:' . $e->getMessage(), compact('message', 'data'));
        }
        $appendData = compact('body', 'addtime', 'eventTag');
        $this->appendData($appendData);
    }

    protected function _exec_record_cache()
    {
        $this->appendLog('订单数据统计事件[执行缓存记录]', $this->data);
        try {
            $body = $this->data['body'];
            $addtime = $this->data['addtime'];
            switch ($this->data['eventTag']) {
                case Event::ORDER_CREATE_SUCCESS:
                    $uid = $body['uid'];
                    $order_parent_no = $body['order_parent_no'];
                    $order_list = $body['order_list'];
                    sDatastat::createOrder($uid, $order_parent_no, $order_list, $addtime);
                    break;
                case Event::ORDER_PAY_SUCCESS:
                    $order_info_list = $body['order_info_list'];
                    sDatastat::payOrder($order_info_list);
                    break;
                case Event::ORDER_REFUND_USER_APPLY_SUCCESS:
                    $order_no = $body['order_no'];
                    sDatastat::refundOrder($order_no);
                    break;
            }
        } catch (\Exception $e) {
            $this->appendLog('订单数据统计事件[记录数据]错误,信息:' . $e->getMessage(), compact('message', 'data'));
        }
    }

}